#!/usr/bin/python3
# -*- coding: utf-8 -*-
'''
@File    :  p2p_data_push.py
@Time    :  2022/05/03 10:45:09
@Author  :  HHLiang
@Contact :  sternapeg@qq.com
@Version :  1.0
@Desc    :  点对点通过新，数据推送
'''
from edp_sdk import *
from config import config_run

if __name__ == '__main__':

    # 以下设备和产品信息，请自行补全
    # api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'    # 设备APIkey
    # product_id = 'XXXXXX'                       # 产品ID
    # device_id = 'XXXXXXXXX'                     # 设备ID
    # auth_info = 'XXXXXXXXXXXX'                  # 鉴权信息
    config = config_run('A1')

    api_key = config[0]
    product_id = config[1]
    device_id = config[2] # 发送设备ID
    auth_info = config[3]

    host = config[4]
    port = config[5]

    # 创建Sockey连接
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 建立连接
    s.connect((host, port))
    # 发送登录报文， 并接收响应
    conn_msg = gen_conn_msg(pid=product_id, auth_info=auth_info)
    print(conn_msg)
    s.send(conn_msg)
    # 接收响应并打印
    resp = s.recv(1024)
    print('设备注册结果：', resp)

    target_id = '935615525'  # 目标设备
    
    # 循环，定时接收命令
    while 1:
        # 对push的数据进行组包
        push_data = gen_push_msg(target_id, '123456789')
        print('PUSH DATA: ', push_data)
        s.send(push_data)
        #延时5秒
        time.sleep(5)
